//
//  HomeNetService.m
//  Shengzhuli
//
//  Created by 卡忙科技 on 16/8/4.
//  Copyright © 2016年 kamangkeji. All rights reserved.
//

#import "HomeNetService.h"
#import "GFXNetworkHandle.h"
#import "SZLAccountHelper.h"
#import "BannerModel.h"
#import "CityGroupModel.h"
#import "SZLAccountHelper.h"
#import "MessageModel.h"
#import "NormalShopGoodsModel.h"
#import "HomeImageModel.h"
#import "AddGoodsModel.h"
#import "HomeGoodsModel.h"
#import "HomeGoodsTypeModel.h"

NSString * const HomeGoodsUrl = @"product/recommend/list"; //首页商品列表
NSString * const BannerListUrl = @"product/normal/bannerList"; //首页轮播图
NSString * const HomeSearchUrl = @"product/normal/search";//搜索
NSString * const CityListUrl = @"city/list";//城市
NSString * const MessageHaveNewUrl = @"auth/notice/get/new/count";//查看有无新消息
NSString * const MessageListUrl = @"auth/notice/list";//消息列表
NSString * const MessageReadUrl = @"auth/notice/update/status";//读消息列表
NSString * const MessageRoolUrl = @"account/user/listRecently";//首页消息轮播
NSString * const HomeFourImageUrl = @"recommend/list";//首页4张图
NSString * const NearWineShopUrl = @"shop/list";//获取首页身边红酒
NSString * const IsNeedUpdate = @"get/app/ios/version";//获取需要更新


NSString * const HomeGoodsType = @"productTypes/listpage";//首页类型
NSString * const HomeModifyGoodsType = @"productTypes/firstList";//修改后首页类型
NSString * const LocationGPS = @"auth/user/GPS";//上传坐标


@implementation HomeNetService

+ (void)getGoodsTypeWithParame:(NSDictionary *)parames response:(void (^)(BOOL success,NSString *msg,NSArray *types))response
{
    NSString *postStr = nil;
    if (parames) {
        postStr = HomeGoodsType;
    }else {
        postStr = HomeModifyGoodsType;
    }
    [[GFXNetworkHandle shareHandle]post:postStr params:parames success:^(id json) {
        if ([json[@"status"] isEqualToString:SuccessStatus]) {
            NSMutableArray *types = [NSMutableArray array];
            for (NSDictionary *dict in json[@"result"]) {
                HomeGoodsTypeModel *model = [HomeGoodsTypeModel objectWithDict:dict];
                [types addObject:model];
            }
            response(YES,json[@"message"],types);
        }else{
            response(NO,json[@"message"],nil);
        }
    } failure:^(NSError *error) {
        response(NO,NetworkError,nil);
    }];
}

+ (void)getHomeGoodsWithParame:(NSDictionary *)parames response:(void (^)(BOOL success,NSString *msg,NSArray *banners))response
{
    [[GFXNetworkHandle shareHandle]post:HomeGoodsUrl params:parames success:^(id json) {
        if ([json[@"status"] isEqualToString:SuccessStatus]) {
            NSMutableArray *goods = [NSMutableArray array];
            for (NSDictionary *dict in json[@"result"]) {
                HomeGoodsModel *model = [HomeGoodsModel objectWithDict:dict];
                [goods addObject:model];
            }
            response(YES,json[@"message"],goods);
        }else{
            response(NO,json[@"message"],nil);
        }
    } failure:^(NSError *error) {
        response(NO,NetworkError,nil);
    }];
}

+ (void)getBannerListWithParame:(NSDictionary *)parames response:(void (^)(BOOL success,NSString *msg,NSArray *banners))response
{
    [[GFXNetworkHandle shareHandle]post:BannerListUrl params:parames success:^(id json) {
        if ([json[@"status"] isEqualToString:SuccessStatus]) {
            NSMutableArray *banners = [NSMutableArray array];
            for (NSDictionary *dict in json[@"result"]) {
                BannerModel *model = [BannerModel bannerWithDict:dict];
                [banners addObject:model];
            }
            response(YES,json[@"message"],banners);
        }else{
            response(NO,json[@"message"],nil);
        }
    } failure:^(NSError *error) {
        response(NO,NetworkError,nil);
    }];
}

+ (void)homeSearchWithParame:(NSDictionary *)parames response:(void (^)(BOOL success,NSString *msg,NSArray *gooods))response
{
    [[GFXNetworkHandle shareHandle]post:HomeSearchUrl params:parames success:^(id json) {
        if ([json[@"status"] isEqualToString:SuccessStatus]) {
            NSMutableArray *sources = [NSMutableArray array];
            for (NSDictionary *dict in json[@"result"]) {
                NormalShopGoodsModel *model = [NormalShopGoodsModel objectWithDict:dict];
                [sources addObject:model];
            }
            response(YES,json[@"message"],sources);
        }else{
            response(NO,json[@"message"],nil);
        }
    } failure:^(NSError *error) {
        NSLog(@"error---%@",error);
        response(NO,NetworkError,nil);
    }];
}

+ (void)getCityListWithresponse:(void (^)(BOOL success,NSString *msg,NSArray *cities))response
{
    [[GFXNetworkHandle shareHandle]post:CityListUrl params:nil success:^(id json) {
        //[json writeToFile:@"/Users/kamangkeji/Desktop/city.plist" atomically:YES];
        if ([json[@"status"] isEqualToString:SuccessStatus]) {
            response(YES,json[@"message"],json[@"result"]);
        }else{
            response(NO,json[@"message"],nil);
        }
    } failure:^(NSError *error) {
        response(NO,NetworkError,nil);
    }];
}

+ (void)messageHaveNewWithResponse:(void (^)(BOOL success,NSString *msg,NSInteger count))response
{
    [[GFXNetworkHandle shareHandle]post:MessageHaveNewUrl params:@{@"token":[SZLAccountHelper shareInstance].token} success:^(id json) {
        if ([json[@"status"] isEqualToString:SuccessStatus]) {
            response(YES,json[@"message"],[json[@"result"][@"count"] integerValue]);
        }else{
            response(NO,json[@"message"],0);
        }
    } failure:^(NSError *error) {
        response(NO,NetworkError,0);
    }];
}

+ (void)getMessageListWithParame:(NSDictionary *)parames response:(void (^)(BOOL success,NSString *msg,NSArray *messages))response
{
    [[GFXNetworkHandle shareHandle]post:MessageListUrl params:parames success:^(id json) {
        if ([json[@"status"] isEqualToString:SuccessStatus]) {
            NSMutableArray *messages = [NSMutableArray array];
            for (NSDictionary *dict in json[@"result"]) {
                MessageModel *model = [MessageModel messageWithDict:dict];
                [messages addObject:model];
            }
            response(YES,json[@"message"],messages);
        }else{
            response(NO,json[@"message"],nil);
        }
    } failure:^(NSError *error) {
        response(NO,NetworkError,nil);
    }];
}

+ (void)messageReadWithParame:(NSDictionary *)parames response:(void (^)(BOOL success,NSString *msg))response
{    [[GFXNetworkHandle shareHandle]post:MessageReadUrl params:parames success:^(id json) {
        if ([json[@"status"] isEqualToString:SuccessStatus]) {
            response(YES,json[@"message"]);
        }else{
            response(NO,json[@"message"]);
        }
    } failure:^(NSError *error) {
        response(NO,NetworkError);
    }];
}
+ (void)getMessageRoolWithParame:(NSDictionary *)parames response:(void (^)(BOOL success,NSString *msg,NSString *content))response
{
    [[GFXNetworkHandle shareHandle]post:MessageRoolUrl params:parames success:^(id json) {
        if ([json[@"status"] isEqualToString:SuccessStatus]) {
            NSArray *results = json[@"result"];
            NSMutableString *content = [NSMutableString string];
            for (NSDictionary *dict in results) {
                [content appendFormat:@"%@    ",dict[@"content"]];
            }
            response(YES,json[@"message"],content);
        }else{
            response(NO,json[@"message"],nil);
        }
    } failure:^(NSError *error) {
        response(NO,NetworkError,nil);
    }];
}
+ (void)getHomeFourImageWithParame:(NSDictionary *)parames response:(void (^)(BOOL success,NSString *msg,NSArray *lists))response
{
    [[GFXNetworkHandle shareHandle]post:HomeFourImageUrl params:parames success:^(id json) {
        if ([json[@"status"] isEqualToString:SuccessStatus]) {
            NSMutableArray *lists = [NSMutableArray array];
            NSDictionary *resultDict = json[@"result"];
            for (int i=1; i<=4; i++) {
                NSDictionary *childDict = resultDict[[NSString stringWithFormat:@"%zd",i]];
                HomeImageModel *model = [HomeImageModel objectWithDict:childDict];
                [lists addObject:model];
            }
            response(YES,json[@"message"],lists);
        }else{
            response(NO,json[@"message"],nil);
        }
    } failure:^(NSError *error) {
        response(NO,NetworkError,nil);
    }];
}

+ (void)getNearWineShopWithParame:(NSDictionary *)parames response:(void (^)(BOOL success,NSString *msg,NSArray *lists,float range))response
{
    [[GFXNetworkHandle shareHandle]post:NearWineShopUrl params:parames success:^(id json) {
        if ([json[@"status"] isEqualToString:SuccessStatus]) {
            NSMutableArray *lists = [NSMutableArray array];
            for (NSDictionary *dict in json[@"result"][@"shopList"]) {
                NearWineShopModel *model = [NearWineShopModel objectWithDict:dict];
                [lists addObject:model];
            }
            response(YES,json[@"message"],lists,[json[@"result"][@"range"] floatValue]);
        }else{
            response(NO,json[@"message"],nil,0);
        }
    } failure:^(NSError *error) {
        response(NO,NetworkError,nil,0);
    }];
}

+ (void)getIsNeedUpdateWithParame:(NSDictionary *)parames response:(void (^)(BOOL success,NSString *msg))response
{
    [[GFXNetworkHandle shareHandle]post:IsNeedUpdate params:parames success:^(id json) {
        if ([json[@"status"] isEqualToString:SuccessStatus]) {
            NSInteger force = [json[@"result"][@"isForce"] integerValue];
            response(YES,[NSString stringWithFormat:@"%zd",force]);
        }else{
            response(NO,json[@"message"]);
        }
    } failure:^(NSError *error) {
        response(NO,NetworkError);
    }];
}

+ (void)upLoadGPSWithParame:(NSDictionary *)parames response:(void (^)(BOOL success,NSString *msg))response
{
    [[GFXNetworkHandle shareHandle]post:LocationGPS params:parames success:^(id json) {
        if ([json[@"status"] isEqualToString:SuccessStatus]) {
            
        }else{
            response(NO,json[@"message"]);
        }
    } failure:^(NSError *error) {
        response(NO,NetworkError);
    }];
}


@end
